<?php

namespace App\Http\Controllers;

use App\BO\Product\ProductBOFactory;
use App\Entity\ProductCategoryEntity;
use App\Events\DistributionPosterProductEvent;
use App\Models\ProductsCategory;
use App\Services\Code\HttpResponse;
use App\Services\ProductService;
use App\ServicesNew\ProductSkuService;
use App\VO\Product\MetaInfo\ProductMetaInfoFactory;
use App\VO\Product\ProductShowVO;

class ProductController extends BaseController
{
    /**
     * @api {get} /product/{productId} 产品详情页
     * @apiGroup Product
     */
    /**
     * @param int $productId
     * @param ProductSkuService $productSkuService
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function show(int $productId, ProductSkuService $productSkuService)
    {
        $productBO = ProductBOFactory::getByProductIdAndCityId($productId, $this->getUserLocationCityId());
        if ($productBO === null) {
            return $this->showJsonResult(HttpResponse::CODE_FAIL, '城市不存在产品');
        }
        $productSkuEntities = $productSkuService->getUseAbleProductSkuEntitiesByProductEntity($productBO->getCityProductInfo());

        $productShowVO = new ProductShowVO();
        $productShowVO->setProduct(ProductMetaInfoFactory::getProductInfo($productBO));

        $skus = [];
        foreach ($productSkuEntities as $productSkuEntity) {
            $skus[] = ProductMetaInfoFactory::getProductSkuInfo($productSkuEntity);
        }
        $productShowVO->setProductSkus($skus);

        //distributor poster
        //$user = $this->getUserEntity();
        //if ($user) {
        //    new DistributionPosterProductEvent($this->getUserLocationCityId(), $user, $productShowVO->product);
        //}

        return $this->successJsonResult($productShowVO);
    }

    /**
     * @api {get} /plist 产品列表
     * @apiGroup Product
     *
     * @apiParam {int} city_id 城市id
     *
     * @apiSuccessExample {json} success:
     *    {"code":200,"message":"success","data":{"\u5bb6\u653f\u4fdd\u6d01":[{"product":{"id":1,"orderImage":"http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/29\/\u65e5\u5e38.png","title":"\u65e5\u5e38\u4fdd\u6d01","bannerImage":"http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/26\/\u56fe\u5c42634@2x.png","productShowType":1,"descriptionImages":["http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/29\/d026910fbfa5b7ddc9388f17bcd10fe47451.png","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/26\/a11e3d158a509395156b862680deb95d4789.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/26\/bbf2bdb18aa3d74dec4afce1031e3f25818.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/26\/46c870c30319545bace31dd77619ab8a7758.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/26\/df064f557803ff10283e70b520187f2e190.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/29\/c9c36c6863bb428ae4114923474addd44032.png","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/26\/f912b2b8ac853574822445e7900adbf61435.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/26\/ea8ed5a72c23956f7d5f9911fa34cdf58457.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/26\/f4627f00666089f58b578000001ab23e8963.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/26\/5d9f2190e3666e5ba3e314cc2afb7d10907.jpg"],"productDesc":"\u5bb6\u5ead\u5ba4\u5185\u8868\u9762\u6e05\u6d01\u3001\u7269\u54c1\u6574\u7406\u3001\u57fa\u7840\u9664\u5c18\u9664\u57a2"},"productSkus":[{"id":578,"title":"\u65e5\u5e38\u4fdd\u6d01","oldPrice":"45.00","Price":"1.00","buyMinNum":1,"buyMaxNum":99,"unit":"\u5c0f\u65f6"}]},{"product":{"id":2,"orderImage":"http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/29\/\u64e6\u73bb\u7483.png","title":"\u64e6\u73bb\u7483","bannerImage":"http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/08\/\u56fe\u5c42 643.jpg","productShowType":2,"descriptionImages":["http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2020\/01\/15\/c21c03ebf51be076555ae89dd28837475981.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/29\/06048207b8284b23b9f4e36533a788d31463.png","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/08\/f13aaf26038b739d9ba554acf4669f474702.png","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/08\/df53a09f2a26d4391d0103cc2888b75a6668.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2020\/01\/15\/8eb4e4cc3b89b08f7ff4d5796ae3aaac8836.jpg"],"productDesc":"\u9ad8\u79d1\u6280\u4e13\u4e1a\u64e6\u6d17\uff0c\u7a97\u6237\u4e5f\u8981\u201cbling bling\u201d\u5c0f\u6e05\u65b0\uff01"},"productSkus":[{"id":54,"title":"\u4e00\u5c45\u5ba4","oldPrice":"150.00","Price":"130.00","buyMinNum":2,"buyMaxNum":9999,"unit":"\u5c45\u5ba4"},{"id":61,"title":"\u4e8c\u5c45\u5ba4","oldPrice":"220.00","Price":"220.00","buyMinNum":1,"buyMaxNum":9999,"unit":"2\u5c45\u5ba4"},{"id":62,"title":"\u4e09\u5c45\u5ba4","oldPrice":"300.00","Price":"300.00","buyMinNum":1,"buyMaxNum":9999,"unit":"3\u5c45\u5ba4"},{"id":63,"title":"\u56db\u5c45\u5ba4","oldPrice":"380.00","Price":"290.00","buyMinNum":1,"buyMaxNum":9999,"unit":"4\u5c45\u5ba4"},{"id":160,"title":"\u5927\u6237\u578b","oldPrice":"15.00","Price":"15.00","buyMinNum":10,"buyMaxNum":9999,"unit":"\u5e73\u7c73"}]},{"product":{"id":17,"orderImage":"http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/29\/\u7cbe\u7ec6.png","title":"\u7cbe\u7ec6\u5f00\u8352","bannerImage":"http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/08\/\u56fe\u5c42 644.jpg","productShowType":1,"descriptionImages":["http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/29\/34e98b01225564619d3926caa6b929193780.png","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/07\/d848698bdcf6b823f562ba90c21ed4c68559.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/07\/24384e4e4faaaefb6dac358f3d5772b27307.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/07\/55ce3ce332851d4b1b5d95cdb25b760e1063.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/07\/c64595a8788b57abe7883a31d32c8bba2464.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/07\/c88b486a5a3d23ad83a8b071f4e13e905546.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/07\/95558c44313c32ba8203e6188943120c5.jpg"],"productDesc":"\u5b89\u5fbd\u7684\u5566\u996d"},"productSkus":[{"id":103,"title":"\u7cbe\u7ec6\u5f00\u8352","oldPrice":"10.00","Price":"10.00","buyMinNum":50,"buyMaxNum":9999,"unit":"\u5e73\u7c73"}]},{"product":{"id":18,"orderImage":"http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/29\/\u5de5\u7a0b\u5f00\u8352.png","title":"\u5de5\u7a0b\u5f00\u8352","bannerImage":"http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/08\/\u56fe\u5c42 636.jpg","productShowType":1,"descriptionImages":["http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/29\/c09bbd71338b3157a61eb2c571a3904a3902.png","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/07\/e76f29c2cd25f284a6436c9ab5f8f5545141.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/07\/08d814b227ce15223a4398fce779f2ef1602.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/07\/4eae74af8c503ec6f2f575d4aa3e2c0d241.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/07\/ecddf4b586a7024d49d66b9d3176f58a3018.jpg"],"productDesc":"\u5de5\u7a0b\u5f00\u83522222"},"productSkus":[{"id":97,"title":"\u5de5\u7a0b\u5f00\u8352","oldPrice":"8.00","Price":"8.00","buyMinNum":50,"buyMaxNum":9999,"unit":"\u5e73\u7c73"}]}],"\u5bb6\u7535\u6e05\u6d17":[{"product":{"id":16,"orderImage":"http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2019\/11\/29\/\u6cb9\u70df\u673a.png","title":"\u6cb9\u70df\u673a\u6e05\u6d17","bannerImage":"http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2020\/01\/17\/48d7c25a5b6c68b98c3e22b28a37b3ab2958.jpg","productShowType":1,"descriptionImages":["http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2020\/04\/01\/7cf058a6d542e96088723d0532ee46e25458.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2020\/04\/01\/7cf058a6d542e96088723d0532ee46e2973.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2020\/04\/01\/7cf058a6d542e96088723d0532ee46e2349.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2020\/04\/01\/7cf058a6d542e96088723d0532ee46e29469.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2020\/04\/01\/7cf058a6d542e96088723d0532ee46e27880.jpg","http:\/\/ljsh.oss-cn-beijing.aliyuncs.com\/pay\/2020\/04\/01\/7cf058a6d542e96088723d0532ee46e21026.jpg"],"productDesc":"\u5168\u9762\u62c6\u5378\u3001\u6df1\u5ea6\u6e05\u6d01\uff0c\u62d2\u7edd\u884c\u4e1a\u201c\u5077\u61d2\u201d\u6f5c\u89c4\u5219\uff01"},"productSkus":[{"id":290,"title":"\u6cb9\u70df\u673a\u6e05\u6d17","oldPrice":"160.00","Price":"100.00","buyMinNum":1,"buyMaxNum":4,"unit":"\u53f0"}]}]}}
     * @apiErrorExample {json} fail
     *    {"code":1100,"message":"缺少城市"}
     */
    public function list(ProductService $productService, ProductSkuService $productSkuService)
    {
        $cityId = request()->input('city_id');
        if (empty($cityId)) {
            return $this->showJsonResult(HttpResponse::CODE_PARAMS_ERR, '缺少城市');
        }
        $products = $productService->getAllProducts();
        if (empty($products)) {
            return $this->successJsonResult([]);
        }

        $category = ProductsCategory::all();
        if (empty($category)) {
            return $this->successJsonResult([]);
        }
        $categoryMap = [];
        foreach ($category as $item) {
            $productCategoryEntity = new ProductCategoryEntity($item);
            $categoryMap[$productCategoryEntity->getId()] = $productCategoryEntity->toArray();
        }

        $ps = [];
        foreach ($products as $product) {
            $productBO = ProductBOFactory::getByProductIdAndCityId($product->getId(), $cityId);
            if ($productBO === null) {
                continue;
            }
            $productSkuEntities = $productSkuService->getUseAbleProductSkuEntitiesByProductEntity($productBO->getCityProductInfo());

            $productShowVO = new ProductShowVO();
            $productShowVO->setProduct(ProductMetaInfoFactory::getProductInfo($productBO));

            $skus = [];
            foreach ($productSkuEntities as $productSkuEntity) {
                $skus[] = ProductMetaInfoFactory::getProductSkuInfo($productSkuEntity);
            }
            //规格信息补全不展示
            if (empty($skus)) {
                continue;
            }
            $productShowVO->setProductSkus($skus);

            $categoryName = $categoryMap[$product->getProductCategoryId()]['category_name'];

            $ps[$categoryName][] = $productShowVO;
        }
        return $this->successJsonResult($ps);
    }
}
